package in.ac.dei.edrp.cms.controller.adddropcourse;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import in.ac.dei.edrp.cms.dao.adddropcourse.AddDropCourseService;
import in.ac.dei.edrp.cms.domain.adddropcourse.AddDropCourseBean;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AddDropCourseController extends MultiActionController {
	private AddDropCourseService addDropCourseService;

	public void setAddDropCourseService(
			AddDropCourseService addDropCourseService) {
		this.addDropCourseService = addDropCourseService;
	}

	public ModelAndView getStudentCourseList(HttpServletRequest request,
			HttpServletResponse response) {
		List<AddDropCourseBean> detailsList = addDropCourseService
				.getStudentCourseList(request.getParameter("regNumber"));
		
		return new ModelAndView("adddropcourse/componentInfo", "detailsList",
				detailsList);
	}

	public ModelAndView getStudentDetailsList(HttpServletRequest request,
			HttpServletResponse response) {
		List<AddDropCourseBean> detailsList = addDropCourseService
				.getProgramDetails(request.getParameter("regNumber"));

		return new ModelAndView("adddropcourse/programInfo", "detailsList",
				detailsList);
	}

	public ModelAndView getTotalCredits(HttpServletRequest request,
			HttpServletResponse response) {
		String credits=addDropCourseService.getTotalCredits(request.getParameter("regNumber"));
		System.out.println(credits);
		return new ModelAndView("enrollment/info", "info", credits);
	}

		public ModelAndView getCourseAvailable(HttpServletRequest request,
				HttpServletResponse response) {
		AddDropCourseBean addDropCourseBean = new AddDropCourseBean();
		addDropCourseBean.setRegNumber(request.getParameter("regNumber"));
		addDropCourseBean.setProgramId(request.getParameter("programId"));
		addDropCourseBean.setBranchId(request.getParameter("branchId"));
		addDropCourseBean.setSpecializationId(request
				.getParameter("specializationId"));
		addDropCourseBean.setSemesterId(request.getParameter("semesterId"));
		addDropCourseBean.setCourseCode(request.getParameter("courseCode"));

		List<AddDropCourseBean> detailsList = addDropCourseService
				.getCourseAvailable(addDropCourseBean);

		return new ModelAndView("adddropcourse/courseInfo", "detailsList",
				detailsList);
	}

	public ModelAndView addDropStudentCourse(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession();

		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		
		AddDropCourseBean dropCourseBean=new AddDropCourseBean();
		dropCourseBean.setRegNumber(request.getParameter("regNumber"));
		dropCourseBean.setCourseCode(request.getParameter("dropcourseCode"));
		dropCourseBean.setSemesterStartDate(request.getParameter("semesterStartDate"));
		dropCourseBean.setSemesterEndDate(request.getParameter("semesterEndDate"));
		
		StringTokenizer courseCodes=new StringTokenizer(request.getParameter("courseCodes"),"|");
		
		List<AddDropCourseBean> addCourseBeans=new ArrayList<AddDropCourseBean>();
		
		while(courseCodes.hasMoreTokens())
		{
			AddDropCourseBean addCourseBean=new AddDropCourseBean();
			
			addCourseBean.setRegNumber(request.getParameter("regNumber"));
			addCourseBean.setCourseGroup(request.getParameter("courseGroup"));
			addCourseBean.setSemesterStartDate(request.getParameter("semesterStartDate"));
			addCourseBean.setSemesterEndDate(request.getParameter("semesterEndDate"));
			addCourseBean.setProgramId(request.getParameter("programId"));
			addCourseBean.setBranchId(request.getParameter("branchId"));
			addCourseBean.setSpecializationId(request.getParameter("specializationId"));
			addCourseBean.setSemesterId(request.getParameter("semesterId"));
			addCourseBean.setRollNumber(request.getParameter("rollNumber"));
			addCourseBean.setEnrollNumber(request.getParameter("enrollNumber"));
			addCourseBean.setCourseCode(courseCodes.nextToken());
			addCourseBean.setStudentId(request.getParameter("studentId"));
			addCourseBean.setUserId(session.getAttribute("userId").toString());
			
			addCourseBeans.add(addCourseBean);
		}
		
		String str=addDropCourseService.addDropStudentCourse(addCourseBeans, dropCourseBean);
		
		return new ModelAndView("enrollment/info", "info", str);
	}
}
